package myJs.data

import myJs.Implicits._
import myJs.data.detailInfo.DetailInfo
import myJs.myPkg._
import myJs.myPkg.jquery._
import myJs.tool.Pojo.FilterContentData
import myJs.tool.Tool
import org.scalajs.dom._
import org.scalajs.dom.ext.Ajax
import scalatags.Text.all._

import scala.collection.immutable.SeqMap
import scala.concurrent.ExecutionContext.Implicits.global
import scala.scalajs.js
import scala.scalajs.js.Dynamic.{global => g}
import scala.scalajs.js.JSConverters._
import scala.scalajs.js.JSON

/**
 * Created by yz on 16/9/2021
 */
object Frequency {

  var tableRs: JArJDJAn = _
  val tabId = "frequency"
  val parentId = s"${tabId} #other"
  val tableId = "table"

  def tableJq = $(s"#${parentId} #${tableId}")

  def init(dicts: JArJDJAn) = {
    tableRs = dicts
    if (dicts != null) {
      initTable(dicts)
      refreshTable
      registerEvent
    }
  }

  def tableFilterEvent = {
    Tool.clickHide

    $(document.body).on("click", s"#${parentId} .myOperate", "", (y: Element) => {
      DetailInfo.filterContentShowFront(y)
    })
    $(document.body).on("click", s"#${parentId} .mySortLi", "", (y: Element) => {
      Tool.execSort(y, parentId, () => {
        refreshTable
      })
    })
    $(document.body).on("click", s"#${parentId} .myConfirm", "", (y: Element) => {
      Tool.confirmFilter(y, parentId, () => {
        refreshTable
      })
    })
    $(document.body).on("click", s"#${parentId} .myHide", "", (y: Element) => {
      Tool.hideFilterContent(y)
    })
    $(document.body).on("click", s"#${parentId} .myRemove", "", (y: Element) => {
      Tool.removeCondition(y, parentId, () => {
        refreshTable
      })
    })
    $(document.body).on("click", s"#${parentId} .myRemoveSort", "", (y: Element) => {
      Tool.removeSort(y, parentId, () => {
        refreshTable
      })
    })
  }

  def registerEvent = {
    tableFilterEvent
  }

  def refreshTable: Any = {
    refreshTable(() => ())
  }

  def refreshTable(f: () => Any): Any = {
    val queryMap = Tool.getQueryMap(parentId)
    val sortData = Tool.sortExec(tableRs, queryMap,getColumnV = getColumnV)
    val filterData = Tool.execFilter(sortData = sortData, queryMap = queryMap, getColumnV = getColumnV)
    tableJq.bootstrapTable("load", filterData)
    f()
  }

  def initFilterContent(tabId: String, map: SeqMap[String, String]) = {
    val sortDatas = map.map { case (k, v) =>
      FilterContentData(k, v)
    }.toList
    Tool.initFilterContent(sortDatas = sortDatas,
      parentId = s"${tabId}")
    sortDatas
  }

  def getColumnV(columnName: String, v: js.Any) = columnName match {
    case _ =>
      v.toString
  }

  def tbFmt(columnName: String): js.Function = (v: js.Any, row: js.Dictionary[js.Any]) => columnName match {
    case x => getColumnV(x, v)
  }

  def initTable(dicts: JArJDJAn) = {

    val map = dicts.head.map { case (k, v) =>
      k
    }.toList.optionMap
    val columnNames = map.keyList
    val allDatas = initFilterContent(parentId, map)
    val columns = columnNames.map { columnName =>
      val title = map.getOrElse(columnName, columnName)
      val fmt = tbFmt(columnName)
      val basicOptions = ColumnOptions.field(columnName).title(title).sortable(false).formatter(fmt)
      columnName match {
        case x if allDatas.map(_.fieldName).contains(x) => basicOptions.
          sortable(false).operateable(true)
        case _ => basicOptions
      }
    }
    val options = TableOptions.columns(columns.toJSArray).searchOnButton(false).filterControl(false).locale("zh-CN").
      search(true)
    tableJq.bootstrapTable(options)
    Tool.dateInit
    Tool.bindEvt()
  }

}
